package com.zxx.pattern.statePattern.state2.usual;

/**
 * 三个状态：空闲、已预订、已入住
 * 四个动作：预定，入住，退订，退房
 * <p>
 * 状态与状态之间可以根据客户的动作来进行转换。定义每个状态的值。
 * 通过客户的动作将每个状态整合起来
 *
 * @author zxx on 2021/1/15
 */
public class ForepartSample {

    public static final int FREEMTIME_STATE = 0;  //空闲状态
    public static final int BOOKED_STATE = 1;     //已预订状态
    public static final int CHECKIN_STATE = 2;    //入住状态

    int state = FREEMTIME_STATE;     //初始状态
    int count = 10;                 //房间数

    //预定
    public void bookRoom() {
        //空闲
        if (state == FREEMTIME_STATE) {
            if (count > 0) {
                System.out.println("空闲房间，完成预订...");

                //改变状态：已预订
                state = BOOKED_STATE;
                count--;

                //房间预订完了,提示客户没有房源了
                if (count == 0) {
                    printNoRoom();
                }
            } else {
                printNoRoom();
            }
        } else if (state == BOOKED_STATE) {
            System.out.println("该房间已经被预订了...");
        } else if(state == CHECKIN_STATE){
            System.out.println("该房间已经有人入住了...");
        }
    }

    //入住
    public void checkInRoom(){
        if (state == FREEMTIME_STATE) {
            if (count > 0) {
                System.out.println("空闲房间，入住。。。");

                //改变状态：已入住
                state = CHECKIN_STATE;
                count--;
                if (count == 0) {
                    printNoRoom();
                }
            } else {
                System.out.println("没房间了");
            }
        } else if (state == BOOKED_STATE) {
            if ("该房间是你预定的".equals("...")) {
                System.out.println("入住");

                state = CHECKIN_STATE;
            } else {
                System.out.println("你没有预定，请预定");
            }
        } else if (state == CHECKIN_STATE) {
            System.out.println("该房间已经入住了...");
        }
    }

    //退订
    public void unsubscribeRoom(){
        if (state == FREEMTIME_STATE) {
            System.out.println("没有预定，无法推定");
        } else if (state == CHECKIN_STATE) {
            System.out.println("有人住，无法推定");

        } else if (state == BOOKED_STATE) {
            System.out.println("已退定");
            state = FREEMTIME_STATE;
            count++;
        }
    }

    //退房
    public void checkOutRoom(){
        if (state == FREEMTIME_STATE) {
            System.out.println("可以不用有这条语句，直接判断state==CHECK_STATE");
        } else if (state == BOOKED_STATE) {

        } else if (state == CHECKIN_STATE) {
            System.out.println("已退房");
            state = FREEMTIME_STATE;
            count++;
        }
    }


    private void printNoRoom() {
        System.out.println("不好意思,房间已经预订完,欢迎您下次光临...");
    }

    //以状态为原子来改变它的行为，而不是通过行为来改变状态呢？于是你就学到了状态模式。

}
